<template>
  <div id="app">
    <van-nav-bar
      fixed
      z-index="2002"
      placeholder
      :title="asyn_title"
      v-if="title!='search-searchData' && title!='search-search' && title!='class' && title!='index' && title!='recruit-mine' && title!='mine'"
      @click-left="leftgo"
      left-arrow
    >
      <template #right>
        <i
          v-if="title=='card'"
          @click="share"
          class="iconfont icon-fenxiang"
          style="color: #666;font-size: .32rem;"
        ></i>
      </template>
    </van-nav-bar>
    <!-- 内容 -->
    <Nuxt v-if="!$route.meta.isKeepAlive && isRouterAlive" />

    <div class="fenxiang" v-if="fenxiang" @click="fenxiangoff">
      <img src="@/assets/img/fenxiang.png" alt />
    </div>
  </div>
</template>


<script>
import routerjs from "./router.js";
import Axios from "axios";
export default {
  provide() {
    return {
      reload: this.reload
    };
  },
  data() {
    return {
      title: this.$route.name, // 路由name
      asyn_title: "", // 显示名称
      fenxiang: false,
      isRouterAlive: true
    };
  },
  watch: {
    $route() {
      this.$store.commit("setToken", localStorage.getItem("token"));
      this.title = this.$route.name;
      if (this.title == "card") {
        this.reload();
      }
      this.metaChange();
    }
  },
  mounted() {
    this.$store.commit("setToken", localStorage.getItem("token"));
    console.log(process.env.BASE_URL, this.$store.state.token);
    this.metaChange();
  },
  methods: {
    // 标题监听
    metaChange() {
      for (let i in routerjs) {
        if (this.title == routerjs[i].name) {
          this.asyn_title = routerjs[i].meta.title;
          return false;
        }
      }
    },
    leftgo() {
      // 如果是完善页面 处于企业登录状态
      if (this.title == "信息登记" && window.localStorage.getItem("qx_token")) {
        this.$router.push({
          path: "/mine/"
        });
        return false;
      }
      // back
      if (window.localStorage.getItem("back")) {
        this.$router.go(-2);
        window.localStorage.removeItem("back");
        return false;
      }
      // 个人名片分享 用户点击进入之后跳转登录 不登录返回操作 返回上一页-个人名片分享
      if (this.$route.query.type == "card") {
        this.$router.go(-1);
        return false;
      }
      // 分享用户左上角返回首页
      if (window.localStorage.getItem("cardBack") == 4) {
        this.$router.push("/");
        window.localStorage.removeItem("cardBack");
        return false;
      }

      // 如果是登录页面 并且不处于登录状态 就返回首页
      if (this.title == "登录" && !window.localStorage.getItem("token")) {
        this.$router.push("/");
        return false;
      }

      if(window.localStorage.getItem('login')){
        this.$router.push("/");
        window.localStorage.removeItem("login");
        return false;
      }

      this.$router.go(-1);
    },
    share() {
      var ua = window.navigator.userAgent.toLowerCase();
      // ua.indexOf('micromessenger')为真-微信端，如果为假，就是其他浏览器
      if (ua.indexOf("micromessenger") > -1) {
        this.fenxiang = !this.fenxiang;
        localStorage.setItem('ua', true)
        return true; // 是微信端
      } else {
        this.$toast("请到微信浏览器内进行分享或发送链接给小伙伴吧~");
        localStorage.removeItem('ua')
        return false;
      }
    },
    fenxiangoff() {
      this.fenxiang = !this.fenxiang;
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      });
    }
    // setSeo() {
    //   if (this.$route.name == "recruit-news") {
    //     return false;
    //   }
    //   this.$axios.post("/home/getConfig", {}).then(res => {
    //     this.seo_keyword = res.data.data.seo_keyword;
    //     this.seo_describe = res.data.data.seo_describe;
    //     this.seo_title = res.data.data.seo_title;
    //   });
    // }
  }
};
</script>

<style>
#app {
  max-width: 600px;
  margin: 0 auto;
}
#app .van-popup {
  z-index: 2006 !important;
}
#app .van-overlay,
#app .van-image-preview__overlay {
  z-index: 2005 !important;
}
#app .van-hairline--bottom,
.van-nav-bar__placeholder {
  height: 0.92rem !important;
}
#app .van-nav-bar .van-icon {
  color: #666;
  font-size: 0.4rem;
}
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
  .footer_nav {
    padding-bottom: 0.68rem !important;
    box-sizing: content-box;
  }
}
</style>
<style scoped>
.fenxiang {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.5);
}
.fenxiang img {
  width: 62%;
  position: absolute;
  right: 10px;
  top: 0;
}
</style>